package com.uzteam.common.version;

import java.util.EnumMap;

/**
 * Created by liyugang on 10/19/15.
 */
public final class Version {
    public static enum DebugOrRelease {
        DEBUG, RELEASE
    }

    public static enum ApiSource {
        INTRANET, EXTRANET
    }

    public static enum Channel {
        DEBUG, UZTEAM, TESTIN, ANZHI, MARKET_91, BAIDU, WANDOUJIA, JIFENG, HIAPK,
        EOEMARKET, SOHU, APPCHINA, MARKET_360, MUMAYI, HUAWEI, GOOGLE, XIAOMI, LENOVO, NDUOA, SOGOU,
        MARKET_3G, PAOJIAO, MEIZU, ALIYUN, WO, MARKET_163, TENCENT;

        private static final EnumMap<Channel, Boolean> sIsReleasedToOutsideMap =
                new EnumMap<Channel, Boolean>(Channel.class);

        static {
            sIsReleasedToOutsideMap.put(DEBUG, false);
            sIsReleasedToOutsideMap.put(UZTEAM, true);
            sIsReleasedToOutsideMap.put(TESTIN, false);
            sIsReleasedToOutsideMap.put(ANZHI, true);
            sIsReleasedToOutsideMap.put(MARKET_91, true);
            sIsReleasedToOutsideMap.put(BAIDU, true);
            sIsReleasedToOutsideMap.put(WANDOUJIA, true);
            sIsReleasedToOutsideMap.put(JIFENG, true);
            sIsReleasedToOutsideMap.put(HIAPK, true);
            sIsReleasedToOutsideMap.put(MARKET_3G, true);
            sIsReleasedToOutsideMap.put(PAOJIAO, true);
            sIsReleasedToOutsideMap.put(MEIZU, true);
            sIsReleasedToOutsideMap.put(ALIYUN, true);
            sIsReleasedToOutsideMap.put(WO, true);
            sIsReleasedToOutsideMap.put(MARKET_163, true);
            sIsReleasedToOutsideMap.put(TENCENT, true);
            sIsReleasedToOutsideMap.put(EOEMARKET, true);
            sIsReleasedToOutsideMap.put(SOHU, true);
            sIsReleasedToOutsideMap.put(APPCHINA, true);
            sIsReleasedToOutsideMap.put(MARKET_360, true);
            sIsReleasedToOutsideMap.put(MUMAYI, true);
            sIsReleasedToOutsideMap.put(HUAWEI, true);
            sIsReleasedToOutsideMap.put(GOOGLE, true);
            sIsReleasedToOutsideMap.put(XIAOMI, true);
            sIsReleasedToOutsideMap.put(LENOVO, true);
            sIsReleasedToOutsideMap.put(NDUOA, true);
            sIsReleasedToOutsideMap.put(SOGOU, true);
            if (Channel.values().length != sIsReleasedToOutsideMap.size()) {
                throw new AssertionError();
            }
        }

        public String printableTextOnUmeng() {
            return toString();
        }

        public boolean isReleasedToOutside() {
            return sIsReleasedToOutsideMap.get(this);
        }
    }

    private Version() {
    }

    private static DebugOrRelease sDebugOrRelease;

    private static ApiSource sApiSource;

    // sChannel is for umeng's statistic.
    private static Channel sChannel;

    private static boolean sInitialized;

    public static void init(final DebugOrRelease debugOrRelease, final ApiSource apiSource,
                            final Channel channel) {
        if (sInitialized) {
            throw new IllegalStateException("Already initialized!");
        }

        validate(debugOrRelease, apiSource, channel);

        sDebugOrRelease = debugOrRelease;
        sApiSource = apiSource;
        sChannel = channel;

        sInitialized = true;
    }

    public static DebugOrRelease getDebugOrRelease() {
        validate();
        return sDebugOrRelease;
    }

    public static ApiSource getApiSource() {
        validate();
        return sApiSource;
    }

    public static Channel getChannel() {
        validate();
        return sChannel;
    }

    private static void validate() {
        if (!sInitialized) {
            throw new IllegalStateException("Not initialized!");
        }
    }

    private static void validate(final DebugOrRelease debugOrRelease, final ApiSource apiSource,
                                 final Channel channel) {
        if (channel.isReleasedToOutside()) {
            if (!debugOrRelease.equals(DebugOrRelease.RELEASE)) {
                throw new IllegalArgumentException(
                        "The channel is to outside but debugOrRelease isn't RELEASE!");
            }
            if (!apiSource.equals(ApiSource.EXTRANET)) {
                throw new IllegalArgumentException(
                        "The channel is to outside but apiSource isn't EXTRANET!");
            }
        }
    }
}